import DistanceTool from './DistanceTool.js';
import AngleTool from './AngleTool.js';
import ElevationTool from './ElevationTool.js';
import LocationTool from './LocationTool.js';
import AreaTool from './AreaTool.js';
import ComponentMultiSelect from './ComponentMultiSelect.js';

export default function tool(viewer) {
	let measureConfig = new Glodon.Bimface.Plugins.Measure.MeasureConfig();
	measureConfig.viewer = viewer;
	viewer.measureTool = new Glodon.Bimface.Plugins.Measure.Measure(measureConfig);
	// viewer.addEventListener(
	//   Glodon.Bimface.Viewer.Viewer3DEvent.MouseClicked,
	//   (data) => {
	//     console.log(data, 'click');
	//     // if (data.eventType === 'RightClick') {
	//     //   viewer.measureTool.switchOff();
	//     // }
	//     console.log(viewer._getLatLonByPosition(data.worldPosition));
	//   }
	// );

	viewer.measureTool.switchOff();
	return {
		activeTool: undefined,
		run(toolId, viewer) {
			if (this.toolList[toolId]) {
				if (this.activeTool) this.activeTool.stop();
				const instance = this.toolList[toolId];
				instance.start();
				this.activeTool = instance;
			}
		},
		close(toolId, viewer) {
			if (this.toolList[toolId]) {
				this.activeTool && this.activeTool.stop();
			}
		},
		toolList: {
			DistanceTool: new DistanceTool(viewer),
			AngleTool: new AngleTool(viewer),
			ElevationTool: new ElevationTool(viewer),
			LocationTool: new LocationTool(viewer),
			AreaTool: new AreaTool(viewer),
			ComponentMultiSelect: new ComponentMultiSelect(viewer)
		},
		getArea() {
			return this.toolList.AreaTool.getArea();
		},
		stopComponentMultiSelect() {
			this.toolList.ComponentMultiSelect.stopComponentMultiSelect();
		},
		closeMeasureTool() {
			viewer.measureTool.switchOff();
			this.activeTool.stop();
		}
	};
}
